/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package upeu.edu.pe.controlador;





//import com.cticsperu.model.SivAcceso;


//import com.cticsperu.model.SivTrabajador;
//import com.cticsperu.service.SivAccesoService;
//import com.cticsperu.service.SivTrabajadorService;
import java.math.BigDecimal;
import upeu.edu.pe.modelo.SauUsuario;
import upeu.edu.pe.servicio.SauAccesoServicio;
import upeu.edu.pe.servicio.SauUsuarioServicio;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import upeu.edu.pe.modelo.SauMenu;
@Controller
public class IndexController {
protected final Log logger = LogFactory.getLog(IndexController.class);
    @Autowired
    //private SivTrabajadorService trabajadorService;  
    private SauUsuarioServicio usuarioServicio;
    @Autowired
    //private SivAccesoService accesoService;
    private SauAccesoServicio accesoServicio;
     
    //------------------------------------------------------------//
    @RequestMapping(value = "login/add", method = RequestMethod.POST)
    public ModelAndView redirecLogin(){
            return new ModelAndView("admin/fm_wlogin"); 
    }
/*
    @RequestMapping(value = "ver/person", method = RequestMethod.GET)
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Map model = new HashMap();
    Long id = Long.parseLong(request.getParameter("personId"));       
    model.put("title", "Update Person"+id);        
    return new ModelAndView("personEdit",model);
    }
    /*
    @RequestMapping(value = "ver/error", method = RequestMethod.GET)
    public ModelAndView validarError(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Map model = new HashMap();
    String message = request.getParameter("message");       
    model.put("title", "Update Person"+message);    
    return new ModelAndView("errorpages/validate",model);
    }   */
    
    @RequestMapping(value = "validar/frame", method = RequestMethod.GET)
    public ModelAndView loginFrame(){      
    return new ModelAndView("admin/indexFrame");
    }

    @RequestMapping(value = "validar/menu", method = RequestMethod.GET)
    public ModelAndView loginMenu(HttpServletRequest request, HttpServletResponse response, BigDecimal String){ 
            SauUsuario usuario=new SauUsuario();            
            Map<String, Object> model = new HashMap<String, Object>();	
         usuario.setIdUsuario((BigDecimal)request.getSession().getAttribute("UserId"));
            List list= usuarioServicio.validarUsuarioId(usuario);
              Iterator iterator = list.iterator();
               if(iterator.hasNext()){
               //SauUsuario to=(SauUsuario)iterator.next();
               /*if(to.getNivel().equals("1") && ((String)request.getSession().getAttribute("NivelFsx")).equals("1")){                
                model.put("accesoN",  accesoService.listaNombreAcceso());
                model.put("imagenN",  "logo.png");*/
                }else{
                model.put("accesoN",  accesoServicio.listaNombreAccesoUsuario(usuario));
                model.put("imagenN",  "logo.png");
                }
                         
    return new ModelAndView("admin/menu", model);
    }
    

    @RequestMapping(value = "validar/home", method = RequestMethod.GET)
    public ModelAndView loginHome(){      
    return new ModelAndView("admin/home");
    }  
    
    @RequestMapping(value = "inicio", method = RequestMethod.GET)
    public ModelAndView inicio(){      
    return new ModelAndView("inicio");
    }
    
    @RequestMapping(value = "validar/logout", method = RequestMethod.GET)
    public ModelAndView loginExit(){      
    return new ModelAndView("admin/detroyxexion");
    }
    
    
    //---------------------------------------------------------------//
   /* @RequestMapping(value = "valor/amor", method = RequestMethod.GET)
    public ModelAndView amorValor(HttpServletRequest request, HttpServletResponse response){ 
        SivTrabajador trabajador=new SivTrabajador();
        trabajador.setIdTrabajador((String)request.getSession().getAttribute("UserId"));
        List list= trabajadorService.amorValor(trabajador);

        logger.info("Tamaño:"+list.size());
             Iterator iterator = list.iterator();             
             while(iterator.hasNext()){
             Object[] tuple = (Object[]) iterator.next();
               logger.info("Acceso id:"+(String) tuple[0]+"|Nombre Acceso:"+(String) tuple[1]+"|Alias Menu:"+(String) tuple[6]);
             }
    return new ModelAndView("modulos/validar");
    }*/
    @RequestMapping(value="menuEstandar",method= RequestMethod.GET)
    public ModelAndView inicioMenuEstandar(){
        logger.info("paso por el index");
        return new ModelAndView("admin/menuEstandar");
    }
    @RequestMapping(value="menuPeriodo",method= RequestMethod.GET)
    public ModelAndView inicioMenuPeriodo(){
        logger.info("paso por el index..");
        return new ModelAndView("admin/menuPeriodo");
    }
    
    @RequestMapping(value="formMenu", method= RequestMethod.GET)
    public ModelAndView irFormularioMenu(@ModelAttribute("SauMenuModelo") SauMenu menuTO,
    BindingResult result){
        return new ModelAndView("formMenu");
        
    }
    
    @RequestMapping(value="guardarFormMenu",method= RequestMethod.POST)
    public ModelAndView gusrdarFormMenu(@ModelAttribute("SauMenuModelo") SauMenu menuTO,
    BindingResult result){
        logger.info("Nombre: " +menuTO.getNombre());
        return  new ModelAndView("redirect:main.html");
    }
    
       @RequestMapping(value="estandar", method= RequestMethod.GET)
    public ModelAndView irEstandar(){
        return new ModelAndView("modulos/estandar/mainEstandar");
    }
    
    @RequestMapping(value="operacion", method= RequestMethod.GET)
    public ModelAndView operacion(){
        return new ModelAndView("redirect:main.html");
    }
    
    @RequestMapping(value="recibeDato", method= RequestMethod.POST)
    public ModelAndView datos(HttpServletRequest r){
        String dato=r.getParameter("nombre");
        logger.info(dato);
        
        return new ModelAndView("redirect:main.html");
    }
      @RequestMapping(value="main", method= RequestMethod.GET)
    public ModelAndView irMain(){
        Map<String, Object> modelo=new HashMap<String, Object>();
        modelo.put("nombre", "acreditacion");
        modelo.put("resultado", "Resultado:"+ (47+55) +"");
        
        logger.info("Resultado:"+(47+55));
        return new ModelAndView("mainPrueba", modelo);
    }
    
}
